﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using USMCdotNET;

namespace USMC.Net
{
    /// <summary>
    /// Interaction logic for ParametersWindow.xaml
    /// </summary>
    public partial class ParametersWindow : Window
    {
        powerParametersPage powerPage;
        manualParametersPage manualPage;
        rotaryTransducerParameters rotaryPage;
        synchronizationPage syncPage;
        baskLashPage backLashPage;
        AccelerationPage accPage;
        Devices _devices;
        
        public ParametersWindow(Devices devices)
        {
            InitializeComponent();
            _devices = devices;
            listBox.SelectionChanged += new SelectionChangedEventHandler(ListBox_SelectionChanged);
            listBox.SelectedIndex = 0;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = listBox.SelectedIndex;
            switch (index)
            {
                case 0:
                    if (powerPage == null)
                        powerPage = new powerParametersPage(_devices);
                    frame.Navigate(powerPage);
                    break;
                case 1:
                    if (manualPage == null)
                        manualPage = new manualParametersPage(_devices);
                    frame.Navigate(manualPage);
                    break;
                case 2:
                    if (rotaryPage == null)
                        rotaryPage = new rotaryTransducerParameters(_devices);
                    frame.Navigate(rotaryPage);
                    break;
                case 3:
                    if (syncPage == null)
                        syncPage = new synchronizationPage(_devices);
                    frame.Navigate(syncPage);
                    break;
                case 4:
                    if (backLashPage == null)
                        backLashPage = new baskLashPage(_devices);
                    frame.Navigate(backLashPage);
                    break;
                case 5:
                    if (accPage == null)
                        accPage = new AccelerationPage(_devices);
                    frame.Navigate(accPage);
                    break;
            }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            if(powerPage!=null)
                powerPage.ApplyParameters();
            if(manualPage!=null)
                manualPage.ApplyParameters();
            if(rotaryPage!=null)
                rotaryPage.ApplyParameters();
            if(syncPage!=null)
                syncPage.ApplyParameters();
            if(backLashPage!=null)
                backLashPage.ApplyParameters();
            if(accPage!=null)
                accPage.ApplyParameters();
            _devices.SetMODE();
            _devices.SetPRMS();
            this.Close();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Вы уверены, что хотите сохранить настройки во флеш-память?","Внимание!", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (powerPage != null)
                    powerPage.ApplyParameters();
                if (manualPage != null)
                    manualPage.ApplyParameters();
                if (rotaryPage != null)
                    rotaryPage.ApplyParameters();
                if (syncPage != null)
                    syncPage.ApplyParameters();
                if (backLashPage != null)
                    backLashPage.ApplyParameters();
                if (accPage != null)
                    accPage.ApplyParameters();
                _devices.SetMODE();
                _devices.SetPRMS();
                // TODO: сохранение
            }
            this.Close();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Вы уверены?", "Подтверждение", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                _devices.GetMODE();
                _devices.GetPRMS();
                this.Close();
            }
        }
    }
}
